<?php
namespace common\components\httpclient;

use common\components\httpclient\Http;
use yii\base\Component;

class CoreAPIClient extends Component {
	public $httpClient;

	public $apiurl;
	public $apiport;

	public function init() {
		parent::init();
		$this->httpClient = new Http($this->apiurl, $this->apiport);
	}

	/**
	 * 采用GET方式请求接口
	 *
	 * ~client:
	 *
	 * 	public function actionGetTest() {
	 *		$coreapi = \Yii::$app->coreapi;
	 *		$apiPath = '/demo/demo/get-demo?businessId=11&haha=1';
	 *		$result = $coreapi->getCoreAPI($apiPath, []);
	 *		echo $result;
	 *	    }
	 *
	 * ~server:
	 *
	 * 	public function actionGetDemo() {
	 *		$params = \Yii::$app->request->get();
	 *		$data = $params;
	 *		$this->returnJson($this->getSuccessArray($data));
	 *	    }
	 *
	 * ~result:
	 *
	 * {
	 *    "status": 0,
	 *    "msg": "请求成功",
	 *     "data": {
	 *            "param1": "11",
	 *            "param2": "22"
	 *        }
	 * }
	 *
	 * @param  $path  接口路径 ex:/demo/demo/get-demo
	 * @param  $headers 自定义header信息
	 * @return 接口返回的数据
	 */
	function getCoreAPI($path, $headers = array()) {
		$headers = $this->buildHeader($headers);
		$headers[] = "GET $path HTTP/1.0";
		$headers[] = "Content-Type: application/json; charset=utf-8";
		$headers[] = "Content-Length: 0";
		$this->httpClient->setHeader($headers);
		\Yii::info("---getCoreAPI---");
		\Yii::info("CoreApi Server:" . $this->apiurl . ':' . $this->apiport);
		\Yii::info("CoreApi path:" . $path);
		$beginTime = microtime(true);
		$result = $this->httpClient->get($path);
		$endTime = microtime(true);
		\Yii::info("response time:" . (($endTime - $beginTime) * 1000) . 'ms');

		if ($result) {
			$result_content = $this->httpClient->getContent();
			\Yii::info("Coreapi result:" . $result_content);
			return $result_content;
		} else {
			\Yii::info("Coreapi result: 请求失败");
			return $result;
		}
	}

	/**
	 * 采用GET方式请求接口,参数采用数组方式提交
	 *
	 * ~client:
	 *
	 * 	public function actionGetTest() {
	 *		$coreapi = \Yii::$app->coreapi;
	 *		$apiPath = '/demo/demo/get-demo?businessId=11&haha=1';
	 *		$data=['businessId'=>'1','haha'=>'1'];
	 *		$result = $coreapi->getCoreAPIWithParams($apiPath,$data, []);
	 *		echo $result;
	 *	    }
	 *
	 * ~server:
	 *
	 * 	public function actionGetDemo() {
	 *		$params = \Yii::$app->request->get();
	 *		$data = $params;
	 *		$this->returnJson($this->getSuccessArray($data));
	 *	    }
	 *
	 * ~result:
	 *
	 * {
	 *    "status": 0,
	 *    "msg": "请求成功",
	 *     "data": {
	 *            "param1": "11",
	 *            "param2": "22"
	 *        }
	 * }
	 *
	 * @param  $path  接口路径 ex:/demo/demo/get-demo
	 * @param  $headers 自定义header信息
	 * @return 接口返回的数据
	 */
	function getCoreAPIWithParams($path, $data = array(), $headers = array()) {
		$headers = $this->buildHeader($headers);
		//$headers[] = "GET $path HTTP/1.0";
		$headers[] = "Content-Type: application/json; charset=utf-8";
		$headers[] = "Content-Length: 0";
		$this->httpClient->setHeader($headers);
		\Yii::info("---getCoreAPIWithParams---");
		\Yii::info("CoreApi Server:" . $this->apiurl . ':' . $this->apiport);
		\Yii::info("CoreApi path:" . $path);
		\Yii::info("CoreApi params:" . $this->httpClient->buildQueryString($data));
		$beginTime = microtime(true);
		$result = $this->httpClient->get($path, $data);
		$endTime = microtime(true);
		\Yii::info("response time:" . (($endTime - $beginTime) * 1000) . 'ms');
		if ($result) {
			$result_content = $this->httpClient->getContent();
			\Yii::info("Coreapi result:" . $result_content);
			return $result_content;
		} else {
			\Yii::info("Coreapi result: 请求失败");
			return $result;
		}
	}

	/**
	 * 采用POST方式请求接口，参数为数组方式
	 *
	 * ~client:
	 *
	 * 	public function actionPostTest() {
	 *		$coreapi = \Yii::$app->coreapi;
	 *		$apiPath = '/demo/demo/post-demo';
	 *		$params = [
	 *			"postParam1" => "11111111",
	 *			"postParam2" => "22222222",
	 *		];
	 *		$result = $coreapi->postCoreAPI($apiPath, $params);
	 *		echo $result;
	 *	}
	 *
	 * ~server:
	 *
	 *	public function actionPostDemo() {
	 *		$getParams = \Yii::$app->request->get();
	 *		$postParams = \Yii::$app->request->post();
	 *		$data = ["getParams" => $getParams, "postParams" => $postParams];
	 *		$this->returnJson($this->getSuccessArray($data));
	 *	}
	 *
	 * ~result:
	 *
	 * {
	 *	    "status": 0,
	 *	    "msg": "请求成功",
	 *	    "data": {
	 *	        "getParams": [],
	 *	        "postParams": {
	 *	            "postParam1": "11111111",
	 *	            "postParam2": "22222222"
	 *	        }
	 *	    }
	 *	}
	 *
	 * @param  $path 接口路径 ex:/demo/demo/post-demo
	 * @param  $data 数组方式的请求参数
	 * @param  $headers 自定义header信息
	 * @return 接口返回的数据
	 */
	function postCoreAPI($path, $data, $headers = array()) {
		$headers = $this->buildHeader($headers);
		$headers[] = "POST $path HTTP/1.0";
		$headers[] = "Content-Type: application/x-www-form-urlencoded; charset=utf-8";
		$this->httpClient->setHeader($headers);
		\Yii::info("---postCoreAPI---");
		\Yii::info("CoreApi Server:" . $this->apiurl . ':' . $this->apiport);
		\Yii::info("CoreApi path:" . $path);
		\Yii::info("CoreApi params:" . $this->httpClient->buildQueryString($data));
		$beginTime = microtime(true);
		$result = $this->httpClient->post($path, $data);
		$endTime = microtime(true);
		\Yii::info("response time:" . (($endTime - $beginTime) * 1000) . 'ms');
		if ($result) {
			$result_content = $this->httpClient->getContent();
			\Yii::info("Coreapi result:" . $result_content);
			return $result_content;
		} else {
			\Yii::info("Coreapi result: 请求失败");
			return $result;
		}
	}

	/**
	 * 采用POST方式请求接口，参数为数组方式
	 *
	 * ~client:
	 *
	 *	public function actionPostjsonTest() {
	 *		$coreapi = \Yii::$app->coreapi;
	 *		$apiPath = '/demo/demo/postjson-demo';
	 *
	 *		$params = [
	 *			"postParam1" => "11111111",
	 *			"postParam2" => "22222222",
	 *		];
	 *
	 *		$result = $coreapi->postJsonCoreAPI($apiPath, json_encode($params));
	 *		echo $result;
	 *	}
	 *
	 * ~server:
	 *
	 * public function actionPostjsonDemo() {
	 *		$getParams = \Yii::$app->request->get();
	 *		$postParams = \Yii::$app->request->post();
	 *		$data = ["getParams" => $getParams, "postParams" => $postParams];
	 *		$this->returnJson($this->getSuccessArray($data));
	 *	}
	 *
	 * ~result:
	 *
	 *	{
	 *	    "status": 0,
	 *	    "msg": "请求成功",
	 *	    "data": {
	 *	        "getParams": [],
	 *	        "postParams": {
	 *	            "postParam1": "11111111",
	 *	            "postParam2": "22222222",
	 *	            "_data_": {
	 *	                "postParam1": "11111111",
	 *	                "postParam2": "22222222"
	 *	            }
	 *	        }
	 *	    }
	 *	}
	 *
	 * @param  $path 接口路径 ex:/demo/demo/postjson-demo
	 * @param  $data 数组方式的请求参数
	 * @param  $headers 自定义header信息
	 * @return 接口返回的数据
	 */
	function postJsonCoreAPI($path, $data, $headers = array()) {
		$headers = $this->buildHeader($headers);
		$headers[] = "POST $path HTTP/1.0";
		$headers[] = "Content-Type: application/json; charset=utf-8";
		$this->httpClient->setHeader($headers);
		\Yii::info("---postJsonCoreAPI---");
		\Yii::info("CoreApi Server:" . $this->apiurl . ':' . $this->apiport);
		\Yii::info("CoreApi path:" . $path);
		$beginTime = microtime(true);
		if (empty($data) || $data === '') {
			$data = "{}";
		}
		\Yii::info("CoreApi params:" . $data);
		$result = $this->httpClient->postJson($path, $data);
		$endTime = microtime(true);
		\Yii::info("response time:" . (($endTime - $beginTime) * 1000) . 'ms');
		if ($result) {
			$result_content = $this->httpClient->getContent();
			\Yii::info("Coreapi result:" . $result_content);
			return $result_content;
		} else {
			\Yii::info("Coreapi result: 请求失败");
			return $result;
		}
	}
	/**
	 * 封装put请求
	 * @param unknown $path
	 * @param unknown $data
	 * @param unknown $headers
	 * @return unknown
	 */
	function putJsonCoreAPI($path, $data, $headers = array()) {
		$headers = $this->buildHeader($headers);
		$headers[] = "PUT $path HTTP/1.0";
		$headers[] = "Content-Type: application/json; charset=utf-8";
		$this->httpClient->setHeader($headers);
		$result = $this->httpClient->postJson($path, $data);
		if ($result) {
			return $this->httpClient->getContent();
		} else {
			return $result;
		}
	}
	/**
	 * 封装delete请求
	 * @param unknown $path
	 * @param unknown $data
	 * @param unknown $headers
	 * @return unknown
	 */
	function deleteJsonCoreAPI($path, $data, $headers = array()) {
		$headers = $this->buildHeader($headers);
		$headers[] = "DELETE $path HTTP/1.0";
		$headers[] = "Content-Type: application/json; charset=utf-8";
		$this->httpClient->setHeader($headers);
		$result = $this->httpClient->postJson($path, $data);
		if ($result) {
			return $this->httpClient->getContent();
		} else {
			return $result;
		}
	}

	/**
	 * 构建header数据
	 * @param unknown $requestHeaders
	 * @return unknown
	 */
	function buildHeader($requestHeaders) {
		$headers = $requestHeaders;
		//TODO:这里加入自定义的一些共通header属性

		$headers[] = "Accept-Encoding: gzip";
		return $headers;
	}

	static function quickGet($url) {
		return Http::quickGet($url);
	}

	static function quickPost($url, $data) {
		return Http::quickPost($url, $data);
	}

}